package com.xexun.gpstracker.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;

import com.xexun.gpstracker.common.Constants;

/**
 * Created by Administrator on 2015/9/11 0011.
 */
public class CameraUtil {

    private Activity activity;
    private String filePath;

    public CameraUtil(Activity activity) {
        this.activity = activity;
    }

    public String getFilePath() {
        return filePath;
    }

    public void openCamera() {
        Intent intentFromCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        filePath = Utils.getAvailableStoragePath() + "bctPhoto.jpg";
        FileUtils.delete(filePath);
        intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(filePath)));
        intentFromCapture.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
        activity.startActivityForResult(intentFromCapture, Constants.CAMERA_REQUEST_CODE);
    }

    /**
     * @return
     * @deprecated 易出现OOM
     */
    public byte[] getPhono() {
        Bitmap bitmap = null;
        ByteArrayOutputStream baos = null;
        try {
            FileInputStream fis = new FileInputStream(filePath);
            bitmap = BitmapFactory.decodeStream(fis);
            baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (null != baos)
            return baos.toByteArray();
        else return null;
    }


}
